Move OmniAuth authentication/callback endpoints to OmniauthCallbacksController.

Akinori MUSHA 9 years ago
parent
commit
5462c62585

+ 15 - 0
app/controllers/omniauth_callbacks_controller.rb

@@ -0,0 +1,15 @@
1
+class OmniauthCallbacksController < Devise::OmniauthCallbacksController
2
+  def action_missing(name)
3
+    case name.to_sym
4
+    when *Devise.omniauth_providers
5
+      service = current_user.services.initialize_or_update_via_omniauth(request.env['omniauth.auth'])
6
+      if service && service.save
7
+        redirect_to services_path, notice: "The service was successfully created."
8
+      else
9
+        redirect_to services_path, error: "Error creating the service."
10
+      end
11
+    else
12
+      raise ActionController::RoutingError, 'not found'
13
+    end
14
+  end
15
+end

+ 0 - 9
app/controllers/services_controller.rb

@@ -33,13 +33,4 @@ class ServicesController < ApplicationController
33 33
       format.json { render json: @service }
34 34
     end
35 35
   end
36
-
37
-  def callback
38
-    @service = current_user.services.initialize_or_update_via_omniauth(request.env['omniauth.auth'])
39
-    if @service && @service.save
40
-      redirect_to services_path, notice: "The service was successfully created."
41
-    else
42
-      redirect_to services_path, error: "Error creating the service."
43
-    end
44
-  end
45 36
 end

+ 3 - 3
config/routes.rb

@@ -66,9 +66,9 @@ Huginn::Application.routes.draw do
66 66
   post  "/users/:user_id/webhooks/:agent_id/:secret" => "web_requests#handle_request" # legacy
67 67
   post  "/users/:user_id/update_location/:secret" => "web_requests#update_location" # legacy
68 68
 
69
-  match '/auth/:provider/callback', to: 'services#callback',
70
-        via: [:get, :post] #, constraints: { provider: Regexp.union(Devise.omniauth_providers.map(&:to_s)) }
71
-  devise_for :users, :sign_out_via => [ :post, :delete ]
69
+  devise_for :users,
70
+             controllers: { omniauth_callbacks: 'omniauth_callbacks' },
71
+             sign_out_via: [:post, :delete]
72 72
 
73 73
   get "/about" => "home#about"
74 74
   root :to => "home#index"

+ 26 - 0
spec/controllers/omniauth_callbacks_controller_spec.rb

@@ -0,0 +1,26 @@
1
+require 'spec_helper'
2
+
3
+describe OmniauthCallbacksController do
4
+  before do
5
+    sign_in users(:bob)
6
+    OmniAuth.config.test_mode = true
7
+    request.env["devise.mapping"] = Devise.mappings[:user]
8
+    request.env["omniauth.auth"] = JSON.parse(File.read(Rails.root.join('spec/data_fixtures/services/twitter.json')))
9
+  end
10
+
11
+  describe "accepting a callback url" do
12
+    it "should update the user's credentials" do
13
+      expect {
14
+        get :twitter
15
+      }.to change { users(:bob).services.count }.by(1)
16
+    end
17
+
18
+    # it "should work with an unknown provider (for now)" do
19
+    #   request.env["omniauth.auth"]['provider'] = 'unknown'
20
+    #   expect {
21
+    #     get :unknown
22
+    #   }.to change { users(:bob).services.count }.by(1)
23
+    #   expect(users(:bob).services.first.provider).to eq('unknown')
24
+    # end
25
+  end
26
+end

+ 0 - 18
spec/controllers/services_controller_spec.rb

@@ -3,8 +3,6 @@ require 'spec_helper'
3 3
 describe ServicesController do
4 4
   before do
5 5
     sign_in users(:bob)
6
-    OmniAuth.config.test_mode = true
7
-    request.env["omniauth.auth"] = JSON.parse(File.read(Rails.root.join('spec/data_fixtures/services/twitter.json')))
8 6
   end
9 7
 
10 8
   describe "GET index" do
@@ -39,20 +37,4 @@ describe ServicesController do
39 37
       }.to raise_error(ActiveRecord::RecordNotFound)
40 38
     end
41 39
   end
42
-
43
-  describe "accepting a callback url" do
44
-    it "should update the user's credentials" do
45
-      expect {
46
-        get :callback, provider: 'twitter'
47
-      }.to change { users(:bob).services.count }.by(1)
48
-    end
49
-
50
-    it "should work with an unknown provider (for now)" do
51
-      request.env["omniauth.auth"]['provider'] = 'unknown'
52
-      expect {
53
-        get :callback, provider: 'unknown'
54
-      }.to change { users(:bob).services.count }.by(1)
55
-      expect(users(:bob).services.first.provider).to eq('unknown')
56
-    end
57
-  end
58 40
 end